/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SellCarUI.java
 *
 * Created on 11-01-2011, 10:14:26
 */
package UILayer.Screens;

import ControlLayer.ActorController;

import ControlLayer.CustomerPickedEvent;
import ControlLayer.LoginStateChangedEvent;
import ControlLayer.PriceCalculator;
import ControlLayer.GlobalInstances;
import ControlLayer.ICustomerPickedEventListener;
import ControlLayer.ILoginStateChangedEventListener;
import ControlLayer.ProductController;
import ControlLayer.SaleController;
import ModelLayer.Actor;
import ModelLayer.Car;
import ModelLayer.Product;
import ModelLayer.SalesLine;
import UILayer.MainGuiView;
import UILayer.SaleCreatedDialog;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Evil
 */
public class SellCarUI extends javax.swing.JPanel {

    javax.swing.table.DefaultTableModel cartModel;
//    ArrayList<Car> cars = new ArrayList<Car>();
    ArrayList<Actor> actors = new ArrayList<Actor>();
    ActorController ac = new ActorController();
    ProductController pcontrol = new ProductController();
    javax.swing.table.DefaultTableModel model;
    ArrayList<Car> allCars;
    ArrayList<Car> filteredCars;
    SaleController sc;

    List<Integer> distinctCarYears;
    MainGuiView mainView;

    /** Creates new form SellCarUI */
    public SellCarUI(MainGuiView mainView)
    {
        initComponents();
        this.mainView = mainView;

        allCars = pcontrol.getProductsInStock();
        filteredCars = new ArrayList<Car>();
        sc = new SaleController();

        sc.createCartOrder(GlobalInstances.getInstance().getCurrentEmployee(), GlobalInstances.getInstance().getCurrentCustomer());

        populateYearJComboBox();
        if (GlobalInstances.getInstance().getCurrentCustomer() != null)
        {
            PresentCustomerInUI(null);
        }

        prepareCartTable();
        populateCarsJTable();
        
        GlobalInstances.getInstance().addICustomerPickedEventListener(new ICustomerPickedEventListener()
        {
            public void CustomerPickedEventOccurred(CustomerPickedEvent evt)
            {
                PresentCustomerInUI(null);
            }
        });

        GlobalInstances.getInstance().addILoginStateChangedEventListener(new ILoginStateChangedEventListener() {

            public void LoginStateChangedEventOccurred(LoginStateChangedEvent evt)
            {
                if (evt.isLoggedIn())
                {
                    assignEmployeeToCart(evt.getEmployee());
                }
            }
        });

        assignEmployeeToCart(GlobalInstances.getInstance().getCurrentEmployee());
    }

    private void assignEmployeeToCart(Actor employee)
    {
        sc.getCartOrder().setEmployee(employee);
    }

    public void populateCarsJTable()
    {
        prepareCarsJTable();
        filteredCars.clear();

        PriceCalculator priceCalc = PriceCalculator.getInstance();

        for (Car car : allCars)
        {
            if (txtSearchCarFilterText.getText().length() > 0)
            {
                String filterText = txtSearchCarFilterText.getText();
                boolean noMatch = true;

                if (stringContains(car.getBrand(), filterText))
                {
                    noMatch = false;
                }
                if (stringContains(car.getModel(), filterText))
                {
                    noMatch = false;
                }

                if (noMatch)
                {
                    continue;
                }
            }

            if (cbYear.getSelectedIndex() > 0)
            {
                if (car.getYear() != distinctCarYears.get(cbYear.getSelectedIndex() - 1))
                {
                    continue;
                }
            }

            filteredCars.add(car);
            car.setSalesPrice(priceCalc.calculatePrice(car.getPurchasePrice()));

            model.addRow(new Object[]
                    {
                        car.getYear(),
                        car.getBrand(),
                        car.getModel(),
                        car.getDoors(),
                        car.getSalesPrice()
                    });
        }
    }

    private boolean stringContains(String lookFor, String value)
    {
        return lookFor.toLowerCase().contains(value.toLowerCase());
    }

    public void prepareCarsJTable()
    {
        model = new javax.swing.table.DefaultTableModel();
        carsJTable.setModel(model);

        model.addColumn("Årgang");
        model.addColumn("Mærke");
        model.addColumn("Model");
        model.addColumn("Antal døre");
        model.addColumn("Pris");
    }

    private void populateYearJComboBox()
    {
        cbYear.removeAllItems();
        cbYear.addItem("Vis alle");
        distinctCarYears = pcontrol.getDistinctCarYears();

        for (int i : distinctCarYears)
        {
            cbYear.addItem(i);
        }
    }
    
    private void prepareCartTable()
    {
        cartModel = new javax.swing.table.DefaultTableModel();
        cartJTable.setModel(cartModel);

        cartModel.addColumn("Årgang");
        cartModel.addColumn("Mærke");
        cartModel.addColumn("Model");
        cartModel.addColumn("Antal døre");
        cartModel.addColumn("Pris");
    }

    private void bindDataToCart()
    {
        ArrayList<SalesLine> sl = sc.getCartOrder().getSalesLines();
        prepareCartTable();
        double FullPrice = 0;

        for(SalesLine salesLine : sl)
        {
            Product product = salesLine.getProduct();

            if(product instanceof Car)
            {
                Car carProduct = (Car) product;

                cartModel.addRow(new Object[]
                        {
                            carProduct.getYear(),
                            carProduct.getBrand(),
                            carProduct.getModel(),
                            carProduct.getDoors(),
                            carProduct.getSalesPrice()
                        });
                FullPrice += carProduct.getSalesPrice();
            }
        }
        
        labFullPrice.setText(String.valueOf(FullPrice) + ",-");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        carsJTable = new javax.swing.JTable();
        addToCartJButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        orderCarsJButton = new javax.swing.JButton();
        cbYear = new javax.swing.JComboBox();
        txtSearchCarFilterText = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtQuantity = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        cartJTable = new javax.swing.JTable();
        finishSaleJButton = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        labFullPrice = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtCustomerEmail = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtCustomerCity = new javax.swing.JTextField();
        txtCustomerZipCode = new javax.swing.JTextField();
        txtCustomerAddress = new javax.swing.JTextField();
        txtCustomerName = new javax.swing.JTextField();
        txtCustomerPhonenumber = new javax.swing.JTextField();
        txtCustomerCountry = new javax.swing.JTextField();
        btnCreateNewCustomer = new javax.swing.JButton();
        btnPickAnotherCustomer = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Nye biler på lager"));
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        carsJTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        carsJTable.setName("carsJTable"); // NOI18N
        jScrollPane1.setViewportView(carsJTable);

        addToCartJButton.setText("Tilføj til indkøbskurv");
        addToCartJButton.setName("addToCartJButton"); // NOI18N
        addToCartJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addToCartJButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Søg efter bilmodel:");
        jLabel1.setName("jLabel1"); // NOI18N

        orderCarsJButton.setText("Bestil ny bil");
        orderCarsJButton.setName("orderCarsJButton"); // NOI18N
        orderCarsJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                orderCarsJButtonActionPerformed(evt);
            }
        });

        cbYear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbYear.setName("cbYear"); // NOI18N
        cbYear.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbYearItemStateChanged(evt);
            }
        });

        txtSearchCarFilterText.setName("txtSearchCarFilterText"); // NOI18N
        txtSearchCarFilterText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtSearchCarFilterTextKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchCarFilterTextKeyReleased(evt);
            }
        });

        jLabel9.setText("År:");
        jLabel9.setName("jLabel9"); // NOI18N

        txtQuantity.setText("1");
        txtQuantity.setName("txtQuantity"); // NOI18N

        jLabel10.setText("Antal:");
        jLabel10.setName("jLabel10"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSearchCarFilterText, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbYear, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(orderCarsJButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 84, Short.MAX_VALUE)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addToCartJButton)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(txtSearchCarFilterText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 366, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addToCartJButton)
                    .addComponent(orderCarsJButton)
                    .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Indkøbskurv"));
        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        cartJTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        cartJTable.setName("cartJTable"); // NOI18N
        jScrollPane2.setViewportView(cartJTable);

        finishSaleJButton.setText("Godkend salg");
        finishSaleJButton.setName("finishSaleJButton"); // NOI18N
        finishSaleJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                finishSaleJButtonActionPerformed(evt);
            }
        });

        jLabel11.setText("Samlet pris:");
        jLabel11.setName("jLabel11"); // NOI18N

        labFullPrice.setText("0,-");
        labFullPrice.setName("labFullPrice"); // NOI18N

        jButton1.setText("Fjern fra indkøbskurv");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labFullPrice)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 205, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(finishSaleJButton)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(finishSaleJButton)
                    .addComponent(jLabel11)
                    .addComponent(labFullPrice)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Kundeinformationer"));
        jPanel3.setName("jPanel3"); // NOI18N

        jLabel2.setText("Navn:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel4.setText("Adresse:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText("Postnummer:");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText("By:");
        jLabel6.setName("jLabel6"); // NOI18N

        txtCustomerEmail.setEditable(false);
        txtCustomerEmail.setName("txtCustomerEmail"); // NOI18N

        jLabel3.setText("Telefonnummer:");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel7.setText("Land:");
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText("Email:");
        jLabel8.setName("jLabel8"); // NOI18N

        txtCustomerCity.setEditable(false);
        txtCustomerCity.setName("txtCustomerCity"); // NOI18N

        txtCustomerZipCode.setEditable(false);
        txtCustomerZipCode.setName("txtCustomerZipCode"); // NOI18N

        txtCustomerAddress.setEditable(false);
        txtCustomerAddress.setName("txtCustomerAddress"); // NOI18N

        txtCustomerName.setEditable(false);
        txtCustomerName.setName("txtCustomerName"); // NOI18N

        txtCustomerPhonenumber.setEditable(false);
        txtCustomerPhonenumber.setName("txtCustomerPhonenumber"); // NOI18N

        txtCustomerCountry.setEditable(false);
        txtCustomerCountry.setName("txtCustomerCountry"); // NOI18N

        btnCreateNewCustomer.setText("Opret ny kunde");
        btnCreateNewCustomer.setName("btnCreateNewCustomer"); // NOI18N
        btnCreateNewCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateNewCustomerActionPerformed(evt);
            }
        });

        btnPickAnotherCustomer.setText("Vælg anden kunde");
        btnPickAnotherCustomer.setName("btnPickAnotherCustomer"); // NOI18N
        btnPickAnotherCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPickAnotherCustomerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addGap(12, 12, 12)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtCustomerCity)
                            .addComponent(txtCustomerZipCode)
                            .addComponent(txtCustomerAddress)
                            .addComponent(txtCustomerName, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtCustomerEmail)
                            .addComponent(txtCustomerCountry, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                            .addComponent(txtCustomerPhonenumber, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(btnCreateNewCustomer)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnPickAnotherCustomer)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCustomerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtCustomerPhonenumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtCustomerAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCustomerEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtCustomerZipCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(txtCustomerCountry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtCustomerCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPickAnotherCustomer)
                    .addComponent(btnCreateNewCustomer))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, 0, 469, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtSearchCarFilterTextKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_txtSearchCarFilterTextKeyPressed
    {//GEN-HEADEREND:event_txtSearchCarFilterTextKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtSearchCarFilterTextKeyPressed

    private void txtSearchCarFilterTextKeyReleased(java.awt.event.KeyEvent evt)//GEN-FIRST:event_txtSearchCarFilterTextKeyReleased
    {//GEN-HEADEREND:event_txtSearchCarFilterTextKeyReleased
        populateCarsJTable();
    }//GEN-LAST:event_txtSearchCarFilterTextKeyReleased

    private void cbYearItemStateChanged(java.awt.event.ItemEvent evt)//GEN-FIRST:event_cbYearItemStateChanged
    {//GEN-HEADEREND:event_cbYearItemStateChanged
        if (evt.getStateChange() == evt.SELECTED)
        {
            populateCarsJTable();
        }
    }//GEN-LAST:event_cbYearItemStateChanged

    private void btnCreateNewCustomerActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCreateNewCustomerActionPerformed
    {//GEN-HEADEREND:event_btnCreateNewCustomerActionPerformed
        this.mainView.AddPane(new createCustomerUI(), "Opret ny kunde");
    }//GEN-LAST:event_btnCreateNewCustomerActionPerformed
    private void addToCartJButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addToCartJButtonActionPerformed
    {//GEN-HEADEREND:event_addToCartJButtonActionPerformed
        int selectedRow = carsJTable.getSelectedRow();
        if(selectedRow > -1)
        {
            int quantity = Integer.parseInt(txtQuantity.getText());

            Car car = filteredCars.get(selectedRow);

            Car newCar = new Car();

            newCar.setCarID(car.getCarID());
            newCar.setBrand(car.getBrand());
            newCar.setDescription(car.getDescription());
            newCar.setDoors(car.getDoors());
            newCar.setModel(car.getModel());
            newCar.setYear(car.getYear());

            newCar.setHk(car.getHk());
            newCar.setEngine(car.getEngine());
            newCar.setFuel(car.getFuel());
            newCar.setColor(car.getColor());
            newCar.setPurchasePrice(car.getPurchasePrice());
            newCar.setSalesPrice(car.getSalesPrice());
            newCar.setQuantity(quantity);

            sc.getCartOrder().addProduct(newCar, quantity);

            bindDataToCart();
        }
    }//GEN-LAST:event_addToCartJButtonActionPerformed


    private void btnPickAnotherCustomerActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnPickAnotherCustomerActionPerformed
    {//GEN-HEADEREND:event_btnPickAnotherCustomerActionPerformed
        this.mainView.ShowPickCustomerDialog();
    }//GEN-LAST:event_btnPickAnotherCustomerActionPerformed

    private void orderCarsJButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_orderCarsJButtonActionPerformed
    {//GEN-HEADEREND:event_orderCarsJButtonActionPerformed
        mainView.AddPane(new OrderCarUI(mainView), "Opret bestilling");
    }//GEN-LAST:event_orderCarsJButtonActionPerformed

    private void finishSaleJButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_finishSaleJButtonActionPerformed
    {//GEN-HEADEREND:event_finishSaleJButtonActionPerformed
        if (GlobalInstances.getInstance().getCurrentCustomer() == null)
        {
            JOptionPane.showMessageDialog(mainView.getFrame(), "Du skal oprette eller vælge en kunde før du kan færdiggøre ordren");
            return;
        }

        SaleCreatedDialog dialog = new SaleCreatedDialog(this.mainView.getFrame(), sc.getCartOrder());
        dialog.setVisible(true);
    }//GEN-LAST:event_finishSaleJButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        int selectedIndex = cartJTable.getSelectedRow();
        sc.getCartOrder().removeProduct(selectedIndex);
        bindDataToCart();
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addToCartJButton;
    private javax.swing.JButton btnCreateNewCustomer;
    private javax.swing.JButton btnPickAnotherCustomer;
    private javax.swing.JTable carsJTable;
    private javax.swing.JTable cartJTable;
    private javax.swing.JComboBox cbYear;
    private javax.swing.JButton finishSaleJButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labFullPrice;
    private javax.swing.JButton orderCarsJButton;
    private javax.swing.JTextField txtCustomerAddress;
    private javax.swing.JTextField txtCustomerCity;
    private javax.swing.JTextField txtCustomerCountry;
    private javax.swing.JTextField txtCustomerEmail;
    private javax.swing.JTextField txtCustomerName;
    private javax.swing.JTextField txtCustomerPhonenumber;
    private javax.swing.JTextField txtCustomerZipCode;
    private javax.swing.JTextField txtQuantity;
    private javax.swing.JTextField txtSearchCarFilterText;
    // End of variables declaration//GEN-END:variables

    private void PresentCustomerInUI(Actor customer)
    {
        Actor currentCustomer;
        if (customer == null)
        {
            currentCustomer = GlobalInstances.getInstance().getCurrentCustomer();
        }
        else
        {
            currentCustomer = customer;
        }

        sc.getCartOrder().setCustomer(currentCustomer);

        txtCustomerAddress.setText(currentCustomer.getAddress());
        txtCustomerCity.setText(currentCustomer.getCity());
        txtCustomerCountry.setText(currentCustomer.getCountry());
        txtCustomerEmail.setText(currentCustomer.getEmail());
        txtCustomerName.setText(currentCustomer.getName());
        txtCustomerPhonenumber.setText(currentCustomer.getPhonenumber());
        txtCustomerZipCode.setText(currentCustomer.getZipCode());
    }
}
